package io.sk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.sk.common.base.BaseResult;
import io.sk.common.base.Constants;
import io.sk.common.utils.CheckDataUtils;
import io.sk.common.utils.MD5Util;
import io.sk.dao.SysUserDao;
import io.sk.entity.PersonnelEntity;
import io.sk.entity.SysUserEntity;
import io.sk.service.PersonnelService;
import io.sk.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 登录账户
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/5/9 11:30
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Autowired
    private PersonnelService personnelService;

    /**
     * @param personnelEntity 注册信息
     * @param type            人员类型：1、教师，2、家长
     * @param password        密码
     * @return BaseResult
     */
    @Override
    public BaseResult register(PersonnelEntity personnelEntity, Integer type, String password)
            throws Exception {

        //人员验证
        BaseResult baseResult = personnelService.checkPersonnel(personnelEntity);
        if (Constants.RESPONSE_CODE_200 != baseResult.getCode()) {
            return baseResult;
        }
        personnelEntity = (PersonnelEntity) baseResult.getData();

        personnelEntity.preInsert(Constants.TAB_CREATE_USER_VALUE);
        personnelEntity.setCreateUser(personnelEntity.getId());
        personnelEntity.setUpdateUser(personnelEntity.getId());
        //人员信息保存
        boolean bl = personnelService.insert(personnelEntity);
        if (!bl) {
            return CheckDataUtils.sendResult605();
        }

        SysUserEntity sysUser = new SysUserEntity();
        BeanUtils.copyProperties(personnelEntity, sysUser);

        sysUser.setPassword(MD5Util.MD5(password));
        if (type == Constants.USER_TEACHER) {
            sysUser.setType(Constants.ACCOUNT_TEACHER);
        } else {
            sysUser.setType(Constants.ACCOUNT_PARENT);
        }
        //账户信息保存
        bl = this.insert(sysUser);
        if (!bl) {
            return CheckDataUtils.sendResult605();
        }

        return CheckDataUtils.sendResult200();
    }

    /**
     * 用户登录
     *
     * @param phone    手机号
     * @param password 密码
     * @return BaseResult
     */
    @Override
    public BaseResult login(String phone, String password) {

        //用户账号查询
        SysUserEntity sysUser = this.selectOne(
                new EntityWrapper<SysUserEntity>().eq("phone", phone)
        );

        //账户信息验证
        if (null == sysUser) {
            return CheckDataUtils.sendResult602();
        }

        //密码验证
        password = MD5Util.MD5(password);
        if (!password.equals(sysUser.getPassword())) {
            return new BaseResult(Constants.RESPONSE_CODE_432, Constants.RESPONSE_MESSAGE_432);
        }

        //账号和密码验证通过后查询对应的用户信息
        PersonnelEntity personnel = personnelService.selectById(sysUser.getId());
        if (null == personnel) {
            return CheckDataUtils.sendResult602();
        }

        return CheckDataUtils.sendResult200(personnel);
    }
}
